﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class UIActivityFestival : UIActivityTemplate
    {
        /// <summary>
        /// 节日活动面板
        /// </summary>

        public UILabel topTips;
        public UILabel activityTime;
        public GameObject jianTou;
        public UIScrollView topScrollView;
        public SimpleGrid topSimpleGrid;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public GameObject[] boys;
        public GameObject[] bgs;
        public GameObject[] itemPrefs;
        public GameObject tabPref;
        public GameObject normalObj = null;
        public GameObject chosedObj = null;
        public GameObject normalContent;
        public GameObject copyContent;
        [HideInInspector]
        Dictionary<int, GameObject> button = new Dictionary<int, GameObject>();
        Dictionary<string, int> tabs = new Dictionary<string, int>();
        string desText;
        int count = 0;//记录红点
        /// <summary>
        /// itemPrefs  说明
        //  多个折扣购买  0
        //  累计登录      1
        //  兑换          2
        //  单个购买      3
        //  单个物品领取  4
        //  多个物品领取  5
        /// </summary>
        protected override void PrepareData()
        {
            //activityType = ActivityType.Festival;
        }
        /// <summary>
        /// 背景人物说明
        /// 0半价
        /// 1折扣
        /// 2限兑
        /// 3限强
        /// 4累计登录
        /// 5开服狂欢
        /// 6庆典活动
        /// 7单充
        /// 8竞技场
        /// 9夺宝
        /// 10悬赏任务 试炼之地
        /// 11悬赏boss
        /// </summary>
        /// <param name="flag"></param>
        public void SetBgAndPerson(int flag)
        {
            for (int i = 0; i < bgs.Length; i++)
            {
                bgs[i].gameObject.SetActive(false);
                boys[i].gameObject.SetActive(false);
            }
            bgs[flag].gameObject.SetActive(true);
            boys[flag].gameObject.SetActive(true);
        }

        public override void Show()
        {
            base.Show();
            CSActivityList msg = new CSActivityList();
            msg.activityId = this.activityId;
            NetAPI.SendHttp(OpDefine.CSActivityList, msg, MsgCallback);
        }
        void MsgCallback(NetWork.Packets.Packet packet, bool success)
        {
            foreach (var item in topSimpleGrid.childs)
                Destroy(item);
            topSimpleGrid.childs.Clear();
            if (success)
            {
                SCActivityList msg = packet.kBody as SCActivityList;
                desText = msg.information;
                int i = 0;
                count = 0;
                tabPref.SetActive(true);
                bool isShow = false;
                jianTou.SetActive(msg.activityList.Count > 5);
                foreach (var item in msg.activityList)
                {
                    string[] strs = item.activityId.Split('_');
                    GameObject tab = Instantiate(tabPref);
                    var script = tab.GetComponent<FestivalTabs>();
                    script.activityId = item.activityId;
                    script.normalLabel.text = item.name;
                    script.chosedLabel.text = item.name;
                    tab.name = item.name;
                    button[i] = tab;
                    bool isUpdate = strs[0].Contains("Update");
                    switch (strs[0])
                    {
                        case "rechargeSingleUpdate":
                        case "rechargeSingleFestival":
                            if(isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.rechargeSingleUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.rechargeSingleFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);           //单充送礼
                            script.type = ActivityType.RechargeSurprise;
                            break;
                        case "totalLoginUpdate":
                        case "totalLoginFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.totalLoginUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.totalLoginFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);          //累计登录
                            script.type = ActivityType.LoginAward;
                            break;
                        case "pvpWinTimesUpdate":
                        case "pvpWinTimesFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.pvpWinTimesUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.pvpWinTimesFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);               //竞技场
                            script.type = ActivityType.BattleRace;
                            break;
                        case "robTreasureTimesUpdate":
                        case "robTreasureTimesFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.robTreasureTimesUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.robTreasureTimesFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);          //夺宝次数
                            script.type = ActivityType.RobTrea;
                            break;
                        case "fightRedNameTimesUpdate":
                        case "fightRedNameTimesFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.fightRedNameTimesUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.fightRedNameTimesFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);       //攻打叛军数
                            script.type = ActivityType.PrizeTask;
                            break;
                        case "killRedNameTimesUpdate":
                        case "killRedNameTimesFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.killRedNameTimesUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.killRedNameTimesFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);          //击杀叛军数
                            script.type = ActivityType.PrizeBoss;
                            break;
                        case "mainCopyWinTimesUpdate":
                        case "mainCopyWinTimesFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.mainCopyWinTimesUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.mainCopyWinTimesFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);         //主线副本     15
                            script.type = ActivityType.MainCopy;
                            break;
                        case "costTotalUpdate":
                        case "costTotalFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.costTotalUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.costTotalFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);                 //累计消费
                            script.type = ActivityType.CostTotal;
                            break;
                        case "rechargeTotalUpdate":
                        case "rechargeTotalFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.rechargeTotalUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.rechargeTotalFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);           //累充奖励 
                            script.type = ActivityType.RechargeTotal;
                            break;
                        case "halfDiscountSaleUpdate":
                        case "halfDiscountSaleFestival":
                            script.type = ActivityType.HalfDiscountSale;
                            break;
                        case "discountSaleUpdate":
                        case "discountSaleFestival":
                            script.type = ActivityType.DiscountSale;
                            break;
                        case "aincradStarsUpdate":
                        case "aincradStarsFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.aincradStarsUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.aincradStarsFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);           //试炼之地
                            script.type = ActivityType.AincradStars;
                            break;
                        case "allCopyDoubleUpdate":
                        case "allCopyDoubleFestival":
                            script.type = ActivityType.CopyBenefits;
                            break;
                        case "openServerSpreeUpdate":
                        case "openServerSpreeFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.openServerSpreeUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.openServerSpreeFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);           //开服狂欢
                            script.type = ActivityType.OpenHappy;
                            break;
                        case "celebrationActivityUpdate":
                        case "celebrationActivityFestival":
                            if (isUpdate)
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.celebrationActivityUpdate);
                            else
                                isShow = StaticData.redTips.GetStateByType(RedPointTip.Red.celebrationActivityFestival);
                            if (isShow)
                                AddRedPoint();
                            script.redPoint.SetActive(isShow);      //庆典活动
                            script.type = ActivityType.Congratulation;
                            break;
                        case "limitExchangeUpdate":
                        case "limitExchangeFestival":
                            script.type = ActivityType.TimeLimitExchange;
                            break;
                        case "limitSaleUpdate":
                        case "limitSaleFestival":
                            script.type = ActivityType.TimeLimitSail;
                            break;
                        default:
                            Debug.LogError("出错啦！id没找到==" + strs[0]);
                            break;
                    }
                    topSimpleGrid.AddChild(tab);
                    tabs[script.type.ToString()] = i++;
                }
                tabPref.SetActive(false);
                ShowTabs();
            }
        }

        void ShowTabs()
        {
            int curTab;
            var page = GetComponentInParent<UIActivity>();
            if (page.options.ContainsKey("tabType"))
            {
                curTab = tabs[page.options["tabType"]];
            }
            else
                curTab = 0;
            button[curTab].GetComponent<FestivalTabs>().OnBtnClick(button[0]);//默认打开第一页
            //page.options.Remove("tabType");
        }

        #region 说明按钮
        public void OnExplainClick()
        {
            PageManager.Instance.OpenPage("DescriptionPage", "");
            var page = UI.PageManager.Instance.currentPage as UI.DescriptionPage;
            if (page != null)
            {
                page.text.text = desText; //.Replace("\\r", "\r").Replace("\\n", "\n")
                page.SetStyle(DescriptionPage.DesType.ActivityFestival);
            }
        }
        #endregion

        //void Start()
        //{
        //    DataMessageTool.AddHandler(DataMessage.RED_POINT, ShowOrHideRedPoint);//红点消息处理委托
        //}

        //void OnDestroy()
        //{
        //    DataMessageTool.RemoveHandler(DataMessage.RED_POINT, ShowOrHideRedPoint);
        //}

        //void ShowOrHideRedPoint(object red)
        //{

        //}
        void AddRedPoint()
        {
            count++;
        }

        public void ReduceRedPoint()
        {
            count--;
            if(count < 1)
            {
                StaticData.redTips.ReduceActivitys();
                if(activityType == ActivityType.Festival)
                    StaticData.redTips.SetPointState(RedPointTip.Red.festival, 0);
                else if(activityType == ActivityType.Update)
                    StaticData.redTips.SetPointState(RedPointTip.Red.update, 0);
            }
        }
    }
}

